<template>
  <div>
    <el-form :model="dataForm" @keyup.enter.native="getDataList()" label-width="80px">
      <el-row :gutter="10">
        <el-col :xs="24" :sm="24" :md="12" :lg="4" :xl="4">
          <el-form-item label="合同编号" prop="contract_number">
            <el-input v-model="dataForm.contract_number" type="text" placeholder="合同编号" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="12" :lg="4" :xl="4">
          <el-form-item label="合同类型" prop="contract_template_id">
            <el-select v-model="dataForm.contract_template_id" >
              <el-option
                v-for="item in contract_template"
                :key="item.id"
                :label="item.title"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="20" :sm="20" :md="6" :lg="4" :xl="4">
          <el-form-item>
            <el-button type="primary" @click="getDataList()">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      :data="dataList"
      border
      :row-class-name="rowClassName"
      v-loading="dataListLoading"
      @sort-change="changeSort"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="contract_number"
        header-align="center"
        width="140"
        align="center"
        label="合同编号">
      </el-table-column>
      <el-table-column
        prop="contract_title"
        header-align="center"
        align="center"
        label="合同名称">
      </el-table-column>
      <el-table-column
        prop="expire"
        header-align="center"
        align="center"
        label="到期时间">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        width="190"
        label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status=== 1" style="color: #F56C6C">待签约</span>
          <span v-else-if="scope.row.status=== 2">签约失败</span>
          <span v-else-if="scope.row.status=== 3">拒签约</span>
          <span v-else-if="scope.row.status=== 4">签约者删除</span>
          <span v-else-if="scope.row.status=== 9">已签约</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="created_at"
        header-align="center"
        align="center"
        width="180"
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="140"
        label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="download(scope.row.id)">合同下载</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          contract_number: '',
          contract_title: '',
          contract_template_id: 0,
          sort_by: '',
          order_by: ''
        },
        contract_template: [{
          id: 0,
          title : '全部',
        }, {
          id: 2,
          title : '个人合同',
        }, {
          id: 3,
          title: '委托书',
        }],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        }
      }
    },
    components: {
    },
    activated () {
      this.getDataList()
    },
    methods: {
      rowClassName({row, rowIndex}) {
        if(row.is_jg) {
          return 'bgred';
        }
        return ''
      },
      changeSort(val) {
        if (val.order !== null) {
          this.dataForm.sort_by = val.prop
          this.dataForm.order_by = val.order === 'ascending' ? 'asc' : 'desc'
        } else {
          this.dataForm.sort_by = ''
          this.dataForm.order_by = ''
        }
        this.dataListLoading = true
        this.getCustomerList()
        this.dataListLoading = false
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.getCustomerList()
        this.dataListLoading = false
      },
      getCustomerList() {
        this.$http({
          url: this.$http.adornUrl('/document/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'contract_template_id': this.dataForm.contract_template_id,
            'contract_number': this.dataForm.contract_number,
            'contract_title': this.dataForm.contract_title,
            'sort': this.dataForm.sort_by,
            'order': this.dataForm.order_by
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      download (_id) {
        this.$http({
          url: this.$http.adornUrl('/document/download/' + _id),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data)
            if(data.fileUrl != '') {
              window.open(data.fileUrl)
            } else {
              this.$message.error("无效的下载链接");
            }
          } else {
            this.$message.error("获取文件下载链接失败");
          }
        })
      },
    }
  }
</script>

<style lang="scss">
  .bgred {
    background-color: #fab6b6 !important;
  }
</style>
